package com.qianfeng.handlers;

import com.qianfeng.dto.Message;
import com.qianfeng.dto.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一的异常处理类
 */
@ResponseBody
@ControllerAdvice
public class BookExceptionHandler {

    @ExceptionHandler(value = {Exception.class})
    public Result commonException(Exception e){

        Result result = new Result();
        Message message = new Message();

        if(e instanceof ArithmeticException){
            message.setMsg("分母不能为0");
            message.setCode(1);
            result.setMessage(message);
        }else{
            message.setMsg("系统异常");
            message.setCode(-1);
            result.setMessage(message);
        }
        return result;
    }
}
